class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

public class demo1 {
    public ListNode reverseList(ListNode head) {
        if(head==null||head.next==null)return head;
        ListNode newhead=null;//设置一个空节点去首次连接 便于操作
        ListNode cur=head;

        while(cur!=null){
            ListNode ncur=cur.next;
            cur.next=newhead;
            newhead=cur;
            cur=ncur;
        }
        return newhead;
    }
}
